package com.archermind.style.controller;

import com.alibaba.fastjson.JSONObject;
import com.archermind.style.bean.User;
import com.archermind.style.pojo.ElementEnum;
import com.archermind.style.pojo.IconTypeEnum;
import com.archermind.style.pojo.TextTypeEnum;
import com.archermind.style.service.UserService;
import com.archermind.style.util.Result;
import com.archermind.style.util.SessionUtil;
import com.archermind.style.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping(value = "/free")
public class LoginAction {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result adminLogin(@RequestParam String account,@RequestParam String password,
                             @RequestParam String verifyCode,HttpSession session) {

        String realverifyCode =  (String)session.getAttribute("verifyCode");
        if(!verifyCode.equalsIgnoreCase(realverifyCode)){
            return Result.error("验证码错误！");
        }
        session.removeAttribute("verifyCode");
        User user = userService.findUserByAccount(account);
        if(user == null){
            return Result.error("帐号不存在！");
        }else if(!user.getPassword().equals(password)){
            return Result.error("帐号或密码错误！");
        }else{
            session.setAttribute("user",user);
            String sessionId = session.getId();
            ServletContext applicationCtx = session.getServletContext();
            applicationCtx.setAttribute(user.getId().toString(), sessionId);
            return Result.success();
        }
    }

    @RequestMapping(value = "/authImage")
    public void service(HttpServletResponse response, HttpSession session) throws ServletException, IOException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        //生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        //删除以前的
        session.removeAttribute("verifyCode");
        session.setAttribute("verifyCode", verifyCode.toLowerCase());
        //生成图片
        int w = 100, h = 32;
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
    }

    @RequestMapping(value = "/logout")
    public Result clearSession(HttpSession session) {
        User user =  (User)session.getAttribute("user");
        if(user !=null){
            ServletContext applicationCtx = session.getServletContext();
            applicationCtx.removeAttribute(user.getId().toString());
            session.removeAttribute("user");
        }
        return Result.success();
    }

    @RequestMapping(value = "/getUserInfo")
    public Result getUserInfo(HttpSession session) {
        User user = SessionUtil.getCurUser();
        if(user == null){
            return Result.error("session 已过期！");
        }
        JSONObject obj = new JSONObject();
        user.setPassword(null);
        obj.put("user",user);
        //obj.put("menus",userService.getMenuTree(user));
        obj.put("elementEnum", ElementEnum.getDict());
        obj.put("iconTypeEnum", IconTypeEnum.getDict());
        obj.put("testTypeEnum", TextTypeEnum.getDict());
        return Result.success(obj);
    }
    
    /**
     * 修改用户密码
     * @return 修改密码的结果
     */
    @RequestMapping(value="/changePassword",method=RequestMethod.POST)
   	public Result handleChangePassword(@RequestParam String oldPass,@RequestParam String newPass ){
        User user = SessionUtil.getCurUser();
        if(!user.getPassword().equals(oldPass)){
            return Result.error("原密码错误！");
        }
        user.setPassword(newPass);
        userService.update(user);
        return Result.success();
    }

}